package com.example.rabbitmq.limit.ack;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author weiyx
 * @date 2021-01-10
 */
public class Consumer {

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        //1,创建工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        //2,获取连接
        Connection connection = connectionFactory.newConnection();
        //3,通过connection创建一个channel
        Channel channel = connection.createChannel();
        //4,exchange, routingkey, queue, 并将queue和交换机绑定
        //设置交换机
        String exchangeName = "test_ack_exchange";
        String routingKey = "ack.#";
        String queueName = "test_ack_queue";

        //声明一个交换机
        channel.exchangeDeclare(exchangeName, "topic", true, false, null);
        //声明一个队列
        channel.queueDeclare(queueName, true, false, false, null);
        //建立关系
        channel.queueBind(queueName, exchangeName, routingKey);

        //5,创建消费者
        //限流方式, autoAck 必须为fale
        channel.basicQos(0, 3, false);
        channel.basicConsume(queueName, false, new MyConsumer(channel));

    }



}
